home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / bbs / pad311.zip / WILDCARD.MH < prev   
Text File  |  1996-05-16  |  3KB  |  130 lines

  1. // This file defines functions for performing regular expression searches on
  2. // strings.
  3. //
  4. // Functions:
  5. //
  6. // bool         matchWildcard   (string: regexp, string: toCompare)
  7. //
  8. //
  9. //
  10. // Description of matchWildcard ()
  11. //
  12. //      Compares two strings. The first string may contain wildcards.
  13. //      Recognised wildcards are:
  14. //
  15. //              *       match any 0 or more characters
  16. //              !       match anything but the following character
  17. //              [...]   match any of the characters within the brackets
  18. //              ?       match any one character
  19. //
  20. //      Returns True if the strings match, False otherwise.
  21. //
  22.  
  23.  
  24. #ifndef __WILDCARD_MH
  25. #define __WILDCARD_MH
  26.  
  27. #ifndef __STRING_MH
  28. #include "string.mh"
  29. #endif
  30.  
  31.  
  32. int matchMultiple (string: theString, string: wildcards, int: i1, Ref int: i2);
  33.  
  34. // This routine is called by matchWildcard. It returns the number of characters
  35. // matched.
  36.  
  37. int matchSubstr (string: theString, string: wildcards, int: i1, Ref int: i2) {
  38.   char: curChar;
  39.  
  40.   curChar := wildcards [i2];
  41.  
  42.   if ((curChar = '?')
  43.     or (curChar = theString [i1])) {
  44.     i2 := i2 + 1;
  45.     return 1;
  46.     }
  47.   else if (curChar = '\\') {
  48.     i2 := i2 + 2;
  49.     if (wildcards [i2-1] = theString [i1]) {
  50.       return 1;
  51.       }
  52.     else return 0;
  53.     }
  54.   else if (curChar = '!') {
  55.     i2 := i2 + 1;
  56.     if (matchSubstr (theString, wildcards,i1,i2) = 0) {
  57.       return 1;
  58.       }
  59.     else return 0;
  60.     }
  61.   else if (curChar = '*') {
  62.     int: temp1, temp2, r;
  63.  
  64.     if (i2 = strlen (wildcards)) {
  65.       i2 := i2 + 1;
  66.       return strlen (theString) - i1 + 1;
  67.       };
  68.  
  69.     for (temp1 := i1; temp1 <= strlen (theString); temp1 := temp1 + 1) {
  70.       temp2 := i2 + 1;
  71.       r := matchMultiple (theString, wildcards, temp1, temp2);
  72.       if (r = strlen (theString) - temp1 + 1) {
  73.         i2 := temp2;
  74.         return r + temp1 - i1;
  75.         };
  76.       };
  77.     return 0;
  78.     }
  79.   else if (curChar = '[') {
  80.     int: found, tmp, temp1;
  81.     found := 0;
  82.     i2 := i2 + 1;
  83.     for (; wildcards [i2] <> ']'; ) {
  84.       tmp := (wildcards [i2] = theString [i1]);
  85.       i2 := i2 + 1;
  86. //      tmp := matchSubstr (theString, wildcards, i1, i2);
  87.       if (tmp > found) {
  88.         found := tmp;
  89.         };
  90.       };
  91.     i2 := i2 + 1;
  92.     return found;
  93.     }
  94.   else {
  95.     i2 := i2 + 1;
  96.     return 0;
  97.     };
  98.   }
  99.  
  100. int matchMultiple (string: theString, string: wildcards, int: i1, Ref int: i2) {
  101.   int: temp1, result, len;
  102.   len := strlen (theString);
  103.  
  104.   for (temp1 := i1; temp1 <= len; ) {
  105.     result := matchSubstr (theString, wildcards, temp1, i2);
  106.     if (result) {
  107.       temp1 := temp1 + result;
  108.       }
  109.     else return temp1 - i1;
  110.     };
  111.   return temp1 - i1;
  112.   }
  113.  
  114. // MatchWildcard compares a string with another string containing wildcards, and
  115. // determines if the two match.
  116.  
  117. bool matchWildcard (string: theString, string: wildcards) {
  118.   int: i1, i2, result;
  119.  
  120.   if (strlen (wildcards) = 0) return False;
  121.  
  122.   i2 := 1;
  123.   if (matchMultiple (theString, wildcards, 1, i2) = strlen (theString))
  124.     if (i2 > strlen (wildcards)) return True;
  125.  
  126.   return False;
  127.   }
  128.  
  129. #endif
  130.